package com.white.androidstudy.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;

/**
 * Created by yutf on 2017/12/7 0007.
 */

public class C extends AbsView
{
    Paint  mLinePaint;
    TextPaint mPaint;
    public C(Context context)
    {
        super(context);

    }

    private void init()
    {
        mPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.BLUE);
        mPaint.setTextAlign(Paint.Align.CENTER);

        mLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mLinePaint.setStyle(Paint.Style.STROKE);
        mLinePaint.setStrokeWidth(1);
        mLinePaint.setColor(Color.YELLOW);
    }

    public C(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init();
    }

    @Override
    public int getDefaultWrapWidth()
    {
        return 100;
    }

    @Override
    public int getDefaultWrapHeight()
    {
        return 100;
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        canvas.drawRect(0.5f,0.5f,getMeasuredWidth()-0.5f, getMeasuredHeight()-0.5f, mLinePaint);

        mPaint.setTextSize(getMeasuredWidth()/2);
        Paint.FontMetrics fm = mPaint.getFontMetrics();
        float baseline = getMeasuredHeight()/2  - fm.ascent - (fm.descent - fm.ascent)/2 ;
        canvas.drawText("Oj", getMeasuredWidth()/2, baseline, mPaint);

        float top = fm.top + baseline;
        float ascent = fm.ascent + baseline;
        float descent = fm.descent + baseline;
        float bottom = fm.bottom + baseline;
        
        mLinePaint.setColor(Color.parseColor("#FF0eabf4"));
        canvas.drawLine(0, ascent, getMeasuredWidth(), ascent, mLinePaint);
        canvas.drawLine(0, top, getMeasuredWidth(), top, mLinePaint);
        mLinePaint.setColor(Color.parseColor("#FFfe3939"));
        canvas.drawLine(0, descent, getMeasuredWidth(), descent, mLinePaint);
        canvas.drawLine(0, bottom, getMeasuredWidth(), bottom, mLinePaint);
        mLinePaint.setColor(Color.parseColor("#FFFFFFFF"));
        canvas.drawLine(0, baseline, getMeasuredWidth(), baseline, mLinePaint);
        mLinePaint.setColor(Color.parseColor("#FF00FF22"));
        canvas.drawLine(0, getMeasuredHeight()/2, getMeasuredWidth(), getMeasuredHeight()/2, mLinePaint);

        float desirew = StaticLayout.getDesiredWidth("Oj", mPaint);
        Log.e("s", "w = " + desirew);
    }

    @Override
    public void onGlobalLayout()
    {

    }
}
